package net.siufung.market.base.dict.controller.regulation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.dict.controller.model.dicttype.DictTypeSaveDto;
import net.siufung.market.base.dict.controller.model.dicttype.DictTypeUpdateDto;
import net.siufung.market.base.dict.mapper.IDictMapper;
import net.siufung.market.base.dict.mapper.IDictTypeMapper;
import net.siufung.market.base.dict.mapper.entity.Dict;
import net.siufung.market.base.dict.mapper.entity.DictType;

import java.util.Objects;

/**
 * @author 陈建峰
 * @date 2023/8/5 10:00 PM
 * @since V1.0.0
 */
public class DictTypeRegulation {

    @AllArgsConstructor
    public static class Save extends AbstractRegulation<DictTypeSaveDto> {

        private static final long serialVersionUID = 1396909626312148157L;

        private final IDictTypeMapper dictTypeMapper;

        @Override
        public boolean isSatisfiedBy(DictTypeSaveDto dictType) {
            VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
            verifyRepeatDto.setMethodEventEnum(MethodEventEnum.ADD);
            verifyRepeatDto.setPk(null);
            verifyRepeatDto.setVerifyKeyValue(dictType.getTypeName());
            if(!dictTypeMapper.checkDictTypeNameNotRepeat(verifyRepeatDto)){
                throw new BizServiceException("字典类型名称重复！");
            }
            verifyRepeatDto.setVerifyKeyValue(dictType.getTypeCode());
            if(!dictTypeMapper.checkDictTypeCodeNotRepeat(verifyRepeatDto)){
                throw new BizServiceException("字典类型编码重复！");
            }
            return Boolean.TRUE;
        }
    }

    @AllArgsConstructor
    public static class Update extends AbstractRegulation<DictTypeUpdateDto> {

        private static final long serialVersionUID = 1396909626312148157L;

        private final IDictTypeMapper dictTypeMapper;

        @Override
        public boolean isSatisfiedBy(DictTypeUpdateDto dictType) {
            VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
            verifyRepeatDto.setMethodEventEnum(MethodEventEnum.UPDATE);
            verifyRepeatDto.setPk(dictType.getId());
            verifyRepeatDto.setVerifyKeyValue(dictType.getTypeName());
            if(!dictTypeMapper.checkDictTypeNameNotRepeat(verifyRepeatDto)){
                throw new BizServiceException("字典类型名称重复！");
            }
            return Boolean.TRUE;
        }
    }

    @AllArgsConstructor
    public static class Remove extends AbstractRegulation<Integer> {

        private static final long serialVersionUID = -2874308489110744103L;

        private final IDictTypeMapper dictTypeMapper;
        private final IDictMapper dictMapper;


        @Override
        public boolean isSatisfiedBy(Integer dictTypeId) {
            if(Objects.isNull(dictTypeId)){
                throw new BizServiceException("字典类型主键不能为空！");
            }
            DictType dictType = dictTypeMapper.selectById(dictTypeId);
            if(Objects.isNull(dictType)){
                throw new BizServiceException("字典类型不存在！");
            }
            if(dictType.getDefaultFlag()){
                throw new BizServiceException("字典类型不能删除！");
            }
            long dictCount = dictMapper.selectCount(
                    new LambdaQueryWrapper<Dict>().eq(Dict::getDictTypeId, dictTypeId));
            if(dictCount > 0){
                throw new BizServiceException("字典类型下存在多条字典不能删除！");
            }
            return Boolean.TRUE;
        }
    }

    @AllArgsConstructor
    public static class Status extends AbstractRegulation<Integer> {

        private static final long serialVersionUID = -2874308489110744103L;

        private final IDictTypeMapper dictTypeMapper;

        @Override
        public boolean isSatisfiedBy(Integer dictTypeId) {
            if(Objects.isNull(dictTypeId)){
                throw new BizServiceException("字典类型主键不能为空！");
            }
            DictType dictType = dictTypeMapper.selectById(dictTypeId);
            if(Objects.isNull(dictType)){
                throw new BizServiceException("字典类型不存在！");
            }
            return Boolean.TRUE;
        }
    }
}
